from django.http import JsonResponse
from django.shortcuts import render, redirect

from worksite import models
from worksite.myfunc import login_wrapper


# 添加软件加固工作量
@login_wrapper
def addmyworksoftware(request):
    # 添加session会话保持
    # 1.获取客户端cookie中的随机字符串
    # 2.去session中查找有没有相同的随机字符串
    # 3.去session对应的Key，value中查看是否有相应的值
    if request.method == "GET":
        sw = models.Software.objects.all()
        allos = models.Os.objects.all()

        return render(request, "add_data/addmyworksoftware.html", {"os": allos, "sw": sw})

    if request.method == "POST":
        ip = request.POST.get("ip")
        date = request.POST.get("date")
        os_id = request.POST.get("os")
        sw_id_list = request.POST.getlist("sw_id")

        mywork_software_id = models.MyworkSoftware.objects.create(ip=ip, date=date, os_id=os_id).id

        for i in sw_id_list:
            models.MyworkSoftwareAndSoftware.objects.create(mywork_software_id=mywork_software_id, software_id=i)
        return redirect("/myworksoftware.html")


# 添加参加主机信息
@login_wrapper
def addhost(request):
    ret = {"status": "True", "message": None}
    ip1 = request.POST.get("ip")
    vendorid = request.POST.get("vendor_id")
    serviceid = request.POST.get("service_id")
    networkid = request.POST.get("network_id")
    # print(ip1, vendorid, serviceid, networkid)

    # 判断IP是否存在
    try:
        tmp_ip = models.Host.objects.get(ip=ip1)
        # print(tmp_ip)
        #     #如果数据库中有则不添加
        if tmp_ip:
            ret['status'] = False
            ret['message'] = "IP已存在"
            return JsonResponse(ret)
    except Exception as e:
        # pass
        if ip1:
            models.Host.objects.create(ip=ip1, vendor_id=vendorid, service_id=serviceid, network_id=networkid)
            return JsonResponse(ret)

    if request.method == "GET":
        vendors = models.Vendor.objects.all()
        services = models.Service.objects.all()
        networks = models.Network.objects.all()
        return render(request, "add_data/add_host.html",
                      {"vendors": vendors, "services": services, "networks": networks})


# 添加账户信息
@login_wrapper
def add_userinfo(request):
    if request.method == "POST":
        tmp_ip = request.POST.get("ip")
        host_id = models.Host.objects.get(ip=tmp_ip).id
        # print(host_id)
        user = request.POST.get("user")
        password = request.POST.get("password")
        rootpwd = request.POST.get("rootpwd")
        management = request.POST.get("management")
        print(ip, user, password, rootpwd, management)
        models.User.objects.create(host_id=host_id, user=user, password=password, management=management,
                                   root_password=rootpwd)
    else:
        return render(request, "add_data/add_userinfo.html")


# 添加基线集古工作
@login_wrapper
def add_myworkbaseline(request):
    if request.method == "POST":
        ip = request.POST.get("ip")
        date = request.POST.get("date")
        os_id = request.POST.get("os")
        models.MyworkBaseline.objects.create(ip=ip, date=date, os_id=os_id)
        return redirect("/baseline.html")
    myworkbaseline_obj = models.MyworkBaseline.objects.all()
    allos = models.Os.objects.all()
    return render(request, "add_data/add_baseline.html", {"baseline_obj": myworkbaseline_obj, "os": allos})
